package com.zxf.basic.view

import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable

/**
 * @author zjc
 * @date 2022/12/28
 */
class ShapeBuilder {
    // 渐变方式：默认从左往右渲染
    var orientation = GradientDrawable.Orientation.LEFT_RIGHT
    // 填充的色值
    var color = Color.TRANSPARENT
    // 渐变的色值
    var colors = intArrayOf()
    // 所有角度的圆角
    var radius = 0f
    // 左上圆角大小
    var radiusTopLeft = 0f
    // 右上圆角大小
    var radiusTopRight = 0f
    // 左下圆角大小
    var radiusBottomLeft = 0f
    // 右下圆角大小
    var radiusBottomRight = 0f
    // 边框大小
    var strokeWidth = 0
    // 边框颜色
    var strokeColor = Color.TRANSPARENT

    fun build(): Drawable {
        val drawable = if (colors.size > 1) GradientDrawable(orientation, colors) else {
            GradientDrawable().apply {
                setColor(this@ShapeBuilder.color)
            }
        }
        if (radius > 0) {
            drawable.cornerRadius = radius
        } else {
            // 四个圆角半径，依次是左上x、左上y、右上x、右上y、右下x、右下y、左下x、左下y
            drawable.cornerRadii = floatArrayOf(
                radiusTopLeft,
                radiusTopLeft,
                radiusTopRight,
                radiusTopRight,
                radiusBottomRight,
                radiusBottomRight,
                radiusBottomLeft,
                radiusBottomLeft
            )
        }
        drawable.setStroke(strokeWidth, strokeColor)
        return drawable
    }
}